import os

from moviepy.editor import VideoFileClip


def videocut(path, start_time, discard_end_time):
    '''
    使用ffmpeg剪辑视频
    :param path: 视频文件夹路径
    :type path: str
    :param start_time: 剪辑视频开始时间点
    :type start_time: str
    :param discard_end_time: 最后多长时间舍弃
    :type discard_end_time: str
    :return:
    :rtype:
    '''
    for root, _, filenames in os.walk(path):
        for filename in filenames:
            if str(filename).lower().endswith('mp4') or str(filename).lower().endswith('avi') or str(
                    filename).lower().endswith('mov') or str(filename).lower().endswith('flv'):
                filepath = os.path.join(root, filename)
                if not os.path.exists(root + '-cut'):
                    os.makedirs(root + '-cut')
                newfilepath = os.path.join(root + '-cut', filename)
                video = VideoFileClip(filepath)
                duration = video.duration
                cmd = f'ffmpeg -y -i {filepath} -ss {start_time} -c copy -t {duration - int(start_time) - int(discard_end_time)} -codec copy  -max_muxing_queue_size 1024 {newfilepath}'
                isRun = os.system(cmd)
                if isRun != 0:
                    return (isRun, "没有安装ffmpeg")


if __name__ == '__main__':
    path = r'D:\like\bilibilivideobypy\徐雅\徐雅正经\xuya-yaojiwu无字幕横屏'
    videocut(path, 0, 30)
